package com.flea.hclv.service.impl;

import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HCarCalendarMapper;
import com.flea.hclv.model.dao.HCarMapper;
import com.flea.hclv.model.entity.HCar;
import com.flea.hclv.model.entity.HCarCalendar;
import com.flea.hclv.model.entity.vo.OrgDeptVo;
import com.flea.hclv.service.HFleetsService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.dao.OrgDeptMapper;
import com.flea.system.entity.OrgDept;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: project
 * @description: 车队
 * @author: shiliqiang
 * @create: 2019-02-19 10:44
 * @version: 1.0
 */
@Service
public class HFleetsServiceImpl implements HFleetsService {

    @Autowired
    private HCarMapper hCarMapper;
    @Autowired
    private HCarCalendarMapper hCarCalendarMapper;
    @Autowired
    private OrgDeptMapper orgDeptMapper;
    @Override
    public ServerResponse<FleaPage<OrgDept>> selectAll(FleaPage<OrgDeptVo> paging, Date startTime, Integer carNumber, String carType, Date endTime) {
        RowBounds rowBounds = new RowBounds(paging.getCurPage(),paging.getPageSize());
        List<HCar> hCars;
        List<OrgDept> byComParams;
        //参数必须都有
        if (StringUtils.isNotBlank(carType)){
            if (carNumber==null|| startTime==null || endTime==null){
                return ServerResponse.createBySuccess();
            }
//            String[] dateFormat = {"yyyy-MM-dd"};
//            Date st = DateUtils.parseDate(startTime,dateFormat);
//            Date end = DateUtils.parseDate(endTime,dateFormat);
            //根据车型 和数量过滤
            hCars = hCarMapper.findTypeAndCarNumber(carNumber,carType,null);
            if (CollectionUtils.isEmpty(hCars)){
                return ServerResponse.createBySuccess();
            }
            //查出 使用开始时间 和使用终止时间内的已经预定车;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String st = sdf.format(startTime);
            String end = sdf.format(endTime);
            List<HCarCalendar> carCalendars = hCarCalendarMapper.selectByTimeAndCarId(hCars,st,end);
            List<String> newCarIds =Lists.newArrayList();
            List<String> carIds = Lists.newArrayList();
            if (CollectionUtils.isEmpty(carCalendars)){
                for (HCar car : hCars){
                    newCarIds.add(car.getId());
                }
            }else {
                //list根据carId分组
                Map<String,Integer> countMap = Maps.newLinkedHashMap();
                Multimap<String,HCarCalendar> multimap = ArrayListMultimap.create();
                for (HCarCalendar carCalendar : carCalendars){
                    multimap.put(carCalendar.getCarId(),carCalendar);
                }
                Map<String, Collection<HCarCalendar>> siteDes = multimap.asMap();
                for (Map.Entry<String, Collection<HCarCalendar>> t : siteDes.entrySet()){
                    countMap.put(t.getKey(),t.getValue().size());
                }
                for (Map.Entry<String,Integer> entry : countMap.entrySet()){
                    if (entry.getValue()>0){
                        carIds.add(entry.getKey());
                    }
                }
                //对比查找 没有车预定的车
//                List<String> newCarIds =Lists.newArrayList();
                String newCarId;
                for (HCar car : hCars){
                    newCarId = car.getId();
                    for (String carId : carIds){

                        if (!carId.equals(newCarId)){
                            newCarIds.add(newCarId);
                        }
                    }
                }
            }
            if (CollectionUtils.isEmpty(newCarIds)){
                FleaPage<OrgDept> fleaPage = new FleaPage<>();
                return ServerResponse.createBySuccess(fleaPage);
            }
            hCars = hCarMapper.selectByCarIds(newCarIds);
            if (CollectionUtils.isEmpty(hCars)){
                return ServerResponse.createBySuccess();
            }
            List<String> uuids = Lists.newArrayList();
            for (HCar car : hCars){
                uuids.add(car.getDeptId());
            }
            byComParams = orgDeptMapper.findByComParams(rowBounds, null, null, null, uuids);
            FleaPage<OrgDept> fleaPage = new FleaPage<>(byComParams);
            return ServerResponse.createBySuccess(fleaPage);
        }else {
            //四个条件都是空，查所有
            if (carNumber==null && startTime==null && endTime==null){
                //所有车队
                byComParams = orgDeptMapper.findByServiceTypePaging(rowBounds, HConstant.D_SERVICE_ORG_FLEET);
                FleaPage<OrgDept> fleaPage = new FleaPage<>(byComParams);
                return ServerResponse.createBySuccess(fleaPage);
            }
            return ServerResponse.createBySuccess();
        }
    }
}
